package de.preiscrawler.parser;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.htmlparser.jericho.Element;
import net.htmlparser.jericho.Source;
import net.htmlparser.jericho.StartTag;
import de.preiscrawler.AlleAnbieter;
import de.preisrobot.entity.Artikel;
import de.preisrobot.service.internal.PreisRobotParserException;

// http://www.datavis.com
// http://www.pcmall.com
public class DatavisSearchParser implements Parser {

    @Override
    public List<Artikel> parse(final String sourceUrlString) throws Exception {

        final Source source = new Source(new URL(sourceUrlString));
        final List<Artikel> res = new ArrayList<Artikel>();
        final List<Artikel> parse = parse(source);
        for (final Artikel artikel : parse) {
            if (artikel.getDesc() != null)
                res.add(artikel);
        }

        return res;
    }

    private List<Artikel> parse(final Source source) throws PreisRobotParserException {
        System.out.println(source);
        final List<Artikel> list = new ArrayList<Artikel>();

        final Element dataElems = findDataRootElement(source);
        final List<Element> allElements2 = dataElems.getAllElements("tr");

        for (final Element productLine : allElements2) {

            final String imagePath = getImage(productLine);
            final String longDesc = getDescription(productLine);
            final String artikelLink = getProductLink(productLine);
            final String priceDollar = getPrice(productLine);

            final Artikel a = new Artikel();
            a.setAnbieterId(getAnbieter());
            a.setArtikelLink(artikelLink);
            a.setArtikelNr("");
            a.setArtikelPreislink(null);
            a.setArtikelPreisRef(null);
            a.setCreationDate(new Date());
            a.setDesc(longDesc);
            a.setHersteller(null);
            a.setImageLink(imagePath);
            a.setPreis(priceDollar);

            list.add(a);
        }

        return list;
    }

    public String getPrice(final Element productLine) {
        try {
            final List<Element> allB = productLine.getAllElements("b");
            allB.get(0);
            final Element price = allB.get(4);
            final String priceDollar = price.getTextExtractor().toString();
            return priceDollar;
        } catch (final Exception e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    private String getProductLink(final Element productLine) {
        try {
            final List<Element> allLinkElem = productLine.getAllElements("a");
            final Element elementProductLink = allLinkElem.get(0);
            final String href = elementProductLink.getAttributeValue("href");
            return href;
        } catch (final Exception e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    private String getDescription(final Element productLine) {
        try {
            final List<Element> descElem = productLine.getAllElements("font");
            final Element longDescElem = descElem.get(0);
            final String longDesc = longDescElem.getTextExtractor().toString();
            return longDesc;
        } catch (final Exception e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    private String getImage(final Element productLine) {
        try {
            final List<Element> allElements3 = productLine.getAllElements("img");
            final Element productImgElem = allElements3.get(0);
            final String imagePath = productImgElem.getAttributeValue("src");
            return imagePath;
        } catch (final Exception e) {
            System.err.println(e.getMessage());
            return null;
        }
    }

    // a from tag with name=Category
    private Element findDataRootElement(final Source source) {
        final List<Element> allElements = source.getAllElements("form");
        for (final Element element : allElements) {
            final String name = element.getAttributeValue("name");
            if (name != null && "category".equals(name)) {
                return element;
            }
        }
        throw new PreisRobotParserException("Could not find <form name= category ");
    }

    /**
     * the content we want is inside
     * <tr height="1" bgcolor="#cccccc" width="@%"">
     * <tr bgcolor="#ffffff">
     * <tr height="1" bgcolor="#cccccc" width="@%"">
     * 
     * @param alleElements
     * @return
     */
    private List<Element> findContentElements(final List<Element> alleElements) {
        final List<Element> contentElements = new ArrayList<Element>();
        for (final Element element : alleElements) {
            final StartTag startTag = element.getStartTag();
            final String tag = startTag.getName();
            if ("tr".equals(tag)) {
                contentElements.add(element);
            }
        }
        return contentElements;
    }

    @Override
    public int getAnbieter() {

        return AlleAnbieter.DATAVIS;
    }

}
